import java.util.concurrent.locks.ReentrantLock;

public class lock_interrupt {
    static ReentrantLock lock1 = new ReentrantLock();
    static ReentrantLock lock2 = new ReentrantLock();
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyThread(lock1, lock2), "线程1");
        Thread t2 = new Thread(new MyThread(lock2, lock1), "线程2");
        t1.start();
        t2.start();
        t1.interrupt();//线程1中断
        System.out.println("main线程完毕");
    }

    static class MyThread extends Thread{
        ReentrantLock lock1;
        ReentrantLock lock2;

        public MyThread(ReentrantLock lock1, ReentrantLock lock2) {
            this.lock1 = lock1;
            this.lock2 = lock2;
        }

        @Override
        public void run() {
            try {
                lock1.lockInterruptibly();//上锁
                System.out.println(Thread.currentThread().getName()+"======");
                Thread.sleep(200);
                lock2.lockInterruptibly();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock1.unlock();
                lock2.unlock();
                System.out.println(Thread.currentThread().getName()+"获取到资源，正常结束。");
            }
        }
    }
}
